layui.use(['table'], function() {
	var $ = layui.jquery;
	var layer = layui.layer;
	var form = layui.form;
	var table = layui.table;
	
    //获取到当前登录的用户
	var user = JSON.parse(sessionStorage.getItem("user"));
    
    var initJobListNo = function(){
    	//页面一加载查询工作单信息并赋值给下拉框
        $.get("delivery/pack/packJobListNo", function(result){
        	for (let i in result) {
        		$("#jobListNo").append(new Option(result[i], result[i]));
        	}
        	form.render('select'); // 更新下拉框
        });
    }
    //加载工作单号
    initJobListNo();
    
    var initPackInfo = function(){
    	//页面一加载查询工作单信息并赋值给下拉框
        $.get("delivery/pack/findAllPackInfo", function(result){
        	var data = result.data;
        	//调用初始化表格方法
            initTable(data);
        });
    }
    //加载工作单号
    initPackInfo();
    
    //初始化表格
    var initTable = function(data){
    	//数据表格渲染
        table.render({
          elem: '#packInformation'
          ,data: data
          ,method: 'get'
          ,where: {} // 你额外要携带数据，以键值对的方式存入
          ,cellMinWidth: 60 // 全局定义所有常规单元格的最小宽度（默认：60）
          ,cols: [[
        	 {type:'numbers'}
            ,{field:'company', title:'单位'}
            ,{field:'packaingProject', title:'包装项目'}
            ,{field:'specifications', title:'规格'}
            ,{field:'grade', title:'等级'}
            ,{field:'serviceCharge', title:'服务费'}
            ,{field:'number', title:'数量'}
            ,{field:'materialCost', title:'材料费'}
          ]]
          ,page: true // 开启分页
          ,limit: 10 // 每页显示的条数
          ,limits: [5,10,20,50,100] // 每页条数的选择项
          ,loading: true // 是否显示加载条(切换分页的时候显示）
          ,id: 'packInformation' // 设定容器唯一 id
          ,even: false // 隔行变色
        });
    }
    
    //全局变量 存储打过包的通过的订单号
    var jobListNo = [];
    
    //下拉框选中事件
    form.on('select(jobListNo)', function(data){
    	//发送请求查询工作单的详细信息
    	$.get("delivery/pack/findPack?jobListNo="+data.value, function(res){
    		$("#jsbzf input[name=factQuantity]").val(res.factQuantity);
    		$("#jsbzf input[name=factWeight]").val(res.factWeight);
    		$("#jsbzf input[name=chargedWeight]").val(res.factWeight);
    		$("#jsbzf input[name=operatingId]").val(user.userName);
    		$("#jsbzf input[name=rukuyuan]").val(user.userName);
    	});
    	//清空
    	$("#jsbzf input[name=size]").val("");
    	$("#jsbzf input[name=bagging]").val("");
    });
    
    // 计算包装费
    $("#jsbzfBtn").click(function(){
    	// 体积
    	var size = $("#jsbzf input[name=size]").val();
    	// 包装
    	var packaingProject = $("#packaingProject").val();
    	//不能为空
    	if (packaingProject == "") {
    		layer.alert("请选择包装");
    		return;
    	} else if (size == "") {
    		layer.alert("请输入体积");
    		return;
    	}
    	$.ajax({
    		async:false,//同步请求
			url:"delivery/pack/findPackInfo",
			type:"get",
			data:{"packaingProject":packaingProject, "specifications":size},
			success:function(res){
				if (res == "" || res == null) {
					layer.alert("该包装不适合这个体积");
					return;
				}
				//包装费赋值
		    	$("#jsbzf input[name=bagging]").val(res.serviceCharge + res.materialCost);
			}
		});
    });
    
    //保存按钮的点击事件
    $("#save").click(function(){
    	var jobListNo = $("#jobListNo").val();
    	var factQuantity = $("#jsbzf input[name=factQuantity]").val();
    	var factWeight = $("#jsbzf input[name=factWeight]").val();
		var chargedWeight = $("#jsbzf input[name=chargedWeight]").val();
		var size = $("#jsbzf input[name=size]").val();
		var bagging = $("#jsbzf input[name=bagging]").val();
		var packaingProject = $("#packaingProject").val();
		//不能为空
    	if (jobListNo == "") {
    		layer.alert("请选择订单");
    		return;
    	} else if (bagging == "") {
    		layer.alert("请计算包装费");
    		return;
    	}
    	//发送请求
    	$.ajax({
    		async:false,//同步请求
			url:"delivery/pack/updatePackInfo",
			type:"post",
			data:{
				"jobListNo":jobListNo,
				"factQuantity":factQuantity,
				"factWeight":factWeight,
				"chargedWeight":chargedWeight,
				"size":size,
				"bagging":bagging,
				"actualPacking":packaingProject,
				"operatingId": user.userId
			},
			success:function(res){
				if (res) {
					layer.msg("保存成功！", {icon: 1, time: 2500}); 
				} else {
					layer.msg("保存失败！", {icon: 2, time: 2500}); 
				}
			}
		});
    });
	
});
